package org.vectrics.recruiter.struts.tag;




import org.apache.ecs.html.A;
import org.vectrics.common.html.*;
import org.vectrics.hibernate.ThreadProperties;

import java.util.*;
import org.apache.log4j.*;
import org.vectrics.recruiter.RecruitServiceLocator;
import org.vectrics.recruiter.job.Job;
import org.vectrics.recruiter.job.JobProvider;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
import java.io.IOException;


public class FeaturedJobsTag extends TagSupport {
    static private transient Logger log = Logger.getLogger(FeaturedJobsTag.class);

    public static String TABLE_FONT = "subFont";


	public int doStartTag() throws JspException  {
        log.debug("doStartTag() called");
		HttpServletRequest request = (HttpServletRequest)this.pageContext.getRequest();
		String contextPath = request.getContextPath();

		Table box = new Table();


        Cell contentCell = box.addRow().addCell();


        Collection featuredJobs = null;

        try {
        	log.debug("doStartTag() - finding featured jobs from database");
        	Set domains = new HashSet();
        	domains.add(ThreadProperties.getDomain());
            featuredJobs = RecruitServiceLocator.getJobProvider().findFeaturedJobs(domains);
        }
        catch (Exception fe) {
            log.error("doStartTag() - exception calling findFeaturedJobs()", fe);
        }

        if ((featuredJobs == null) || (featuredJobs.size() == 0)) {
            contentCell.addElement(TextHelper.getFont("stdFont", "<br><i>No Featured Jobs</i>"));
        }
        else {
            log.debug("doStartTag() - making table to display jobs");
            Table listTable = new Table();
            listTable.setCellPadding(3);
            listTable.setWidth("100%");
            contentCell.addElement(listTable);
            Iterator iter = featuredJobs.iterator();
            while (iter.hasNext()) {
                Job job = (Job)iter.next();
                Row row = listTable.addRow();
                Cell cell = row.addCell();
                cell.setWidth("75%");


                A titleLink = new A();
                titleLink.setClass("lnkArticle");
                titleLink.setHref(contextPath + "/public/" + ThreadProperties.getUriDomainId() + "/job/job_view.do"
                    + "?jobId=" + job.getId());
                titleLink.addElement(job.getTitle());
                cell.addElement(titleLink);
/*
				cell = row.addCell();
				cell.setWidth("10%");

                cell = row.addCell();
                cell.setWidth("15%");
                A viewLink = new A();
                viewLink.setClass("lnkSubHover");
                viewLink.setHref(contextPath + "/public/job/job_view.do"
                    + "?id=" + job.getId());
                viewLink.addElement(this.getPhrase("phrase.view"));
                cell.addElement(viewLink);
                */
            }
        }


		JspWriter writer = pageContext.getOut();
		try {
			log.debug("doStartTag() - writing to output");
			writer.print(box.toString());
		}
		catch (IOException ex) {
			log.error("doStartTag() - exception writing output", ex);
			throw new JspException("IOException attempting to write tag to output. " + ex.getMessage());
		}
		log.debug("doStartTag() - done");
		return(EVAL_PAGE);

    }

}
